package com.zelin.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.zelin.shop.entity.BrandEntity;
import com.zelin.shop.entity.GoodsDescEntity;
import com.zelin.shop.entity.ItemEntity;
import com.zelin.shop.entity.group.Goods;
import com.zelin.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.shop.dao.GoodsDao;
import com.zelin.shop.entity.GoodsEntity;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private ItemService itemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    // 添加商品
    @Override
    public void insert(Goods goods) {
        // 添加spu商品
        GoodsEntity goodsEntity = goods.getGoods();
        goodsEntity.setAuditStatus("0");   //未审核
        goodsEntity.setSellerId("zelin");  //商家id
        baseMapper.insert(goodsEntity);
        // 添加spu商品描述（tb_goodsDesc）
        GoodsDescEntity goodsDesc = goods.getGoodsDesc();
        goodsDesc.setGoodsId(goodsEntity.getId());
        goodsDescService.save(goodsDesc);
        //添加sku商品列表（tb_items）
        insertItems(goods);
    }

    //根据商品id查询组合对象
    @Override
    public Goods findGoodsById(Long id) {
        //3.1 查询出spu商品对象
        GoodsEntity goodsEntity = baseMapper.selectById(id);

        //3.2 查询出spu商品描述对象
        GoodsDescEntity goodsDescEntity = goodsDescService.getById(id);

        //3.3 查询出sku商品列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));

        //3.4 定义组合对象
        Goods goods = new Goods();
        goods.setGoods(goodsEntity);
        goods.setGoodsDesc(goodsDescEntity);
        goods.setItems(items);

        return goods;
    }
    //4. 修改商品
    @Override
    public void update(Goods goods) {
        //4.1 修改spu商品表
        baseMapper.updateById(goods.getGoods());

        //4.2 修改spu商品描述表
        goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
        goodsDescService.updateById(goods.getGoodsDesc());

        //4.3 根据goods_id在tb_item表中删除中间表数据
        itemService.remove(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));

        //4.4 向中间表添加数据
        insertItems(goods);
    }

    // 添加sku商品列表
    private void insertItems(Goods goods) {
        for (ItemEntity item: goods.getItems()) {
            // 设置外键字段
            item.setGoodsId(goods.getGoods().getId());
            BrandEntity brand = brandService.getById(goods.getGoods().getBrandId());
            // 设置品牌名
            item.setBrand(brand.getName());
            // 设置商家名称
            item.setSeller(sellerService.getById(goods.getGoods().getSellerId()).getSellerId());
            // 设置标题
            item.setTitle(goods.getGoods().getGoodsName());
            // 设置图像
            GoodsDescEntity goodsDesc = goodsDescService.getById(goods.getGoods().getId());
            String itemImages = goodsDesc.getItemImages();
            List<Map> map = JSON.parseArray(itemImages, Map.class);
            Object url = map.get(0).get("url");
            item.setImage(url + "");
            // 设置创建及修改时间及分类名称
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setCategory(itemCatService.getById(goods.getGoods().getCategory3Id()).getName());
            // 设置状态
            item.setStatus("0");
            item.setCategoryid(goods.getGoods().getCategory3Id());
            // 添加sku商品
            itemService.save(item);
        }
    }

}